package com.irdstudio.tdp.executor.core.dao;

import com.irdstudio.tdp.executor.core.dao.domain.MigrateIndexResult;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 迁移校验指标校验结果类-[表名: 迁移校验指标校验结果(migrate_index_result)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2020-11-13
 */
public class MigrateIndexResultDao {

	/* 连接对象 */
	Connection conn = null;	

	public MigrateIndexResultDao(Connection conn){
		this.conn = conn;
	}


    /**
	* 新增一行
	* @return int
	*/
    public int insertMigrateIndexResult(MigrateIndexResult migrateIndexResult) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("insert into migrate_index_result ( "
                + "check_result_id,check_index_id,subs_code,check_index_name,check_org_code,check_org_name,source_table_id,source_table_code,source_table_name,source_index_value,target_table_id,target_table_code,target_table_name,target_index_value,check_expect_flag"
                + ") values ("+ "?,?,?,?,?,?,?,?,?,?,?,?,?,?,?" +")"
                );
            ps.setObject(1, migrateIndexResult.getCheckResultId());
            ps.setObject(2, migrateIndexResult.getCheckIndexId());
            ps.setObject(3, migrateIndexResult.getSubsCode());
            ps.setObject(4, migrateIndexResult.getCheckIndexName());
            ps.setObject(5, migrateIndexResult.getCheckOrgCode());
            ps.setObject(6, migrateIndexResult.getCheckOrgName());
            ps.setObject(7, migrateIndexResult.getSourceTableId());
            ps.setObject(8, migrateIndexResult.getSourceTableCode());
            ps.setObject(9, migrateIndexResult.getSourceTableName());
            ps.setObject(10, migrateIndexResult.getSourceIndexValue());
            ps.setObject(11, migrateIndexResult.getTargetTableId());
            ps.setObject(12, migrateIndexResult.getTargetTableCode());
            ps.setObject(13, migrateIndexResult.getTargetTableName());
            ps.setObject(14, migrateIndexResult.getTargetIndexValue());
            ps.setObject(15, migrateIndexResult.getCheckExpectFlag());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("insert MigrateIndexResult is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键删除一行记录
	* @return int
	*/
    public int deleteByPk(MigrateIndexResult migrateIndexResult) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("delete from migrate_index_result where 1=1"
                + " AND check_result_id = ? "
                );
            ps.setObject(1, migrateIndexResult.getCheckResultId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("delete MigrateIndexResult is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键更新一行记录
	* @return int
	*/
    public int updateByPk(MigrateIndexResult migrateIndexResult) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("update migrate_index_result set "
                + " check_result_id = ? ,"
                + " check_index_id = ? ,"
                + " subs_code = ? ,"
                + " check_index_name = ? ,"
                + " check_org_code = ? ,"
                + " check_org_name = ? ,"
                + " source_table_id = ? ,"
                + " source_table_code = ? ,"
                + " source_table_name = ? ,"
                + " source_index_value = ? ,"
                + " target_table_id = ? ,"
                + " target_table_code = ? ,"
                + " target_table_name = ? ,"
                + " target_index_value = ? ,"
                + " check_expect_flag = ? "
                + " where 1=1"
                + " AND check_result_id = ? "
                );
            ps.setObject(1, migrateIndexResult.getCheckResultId());
            ps.setObject(2, migrateIndexResult.getCheckIndexId());
            ps.setObject(3, migrateIndexResult.getSubsCode());
            ps.setObject(4, migrateIndexResult.getCheckIndexName());
            ps.setObject(5, migrateIndexResult.getCheckOrgCode());
            ps.setObject(6, migrateIndexResult.getCheckOrgName());
            ps.setObject(7, migrateIndexResult.getSourceTableId());
            ps.setObject(8, migrateIndexResult.getSourceTableCode());
            ps.setObject(9, migrateIndexResult.getSourceTableName());
            ps.setObject(10, migrateIndexResult.getSourceIndexValue());
            ps.setObject(11, migrateIndexResult.getTargetTableId());
            ps.setObject(12, migrateIndexResult.getTargetTableCode());
            ps.setObject(13, migrateIndexResult.getTargetTableName());
            ps.setObject(14, migrateIndexResult.getTargetIndexValue());
            ps.setObject(15, migrateIndexResult.getCheckExpectFlag());
            ps.setObject(16, migrateIndexResult.getCheckResultId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("update MigrateIndexResult is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }

	}

    /**
	* 根据主键查询一行记录
	* @return int
	*/
    public MigrateIndexResult queryByPk(MigrateIndexResult migrateIndexResult) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        MigrateIndexResult result = null;
        try {
            ps = conn.prepareStatement("select check_result_id,check_index_id,subs_code,check_index_name,check_org_code,check_org_name,source_table_id,source_table_code,source_table_name,source_index_value,target_table_id,target_table_code,target_table_name,target_index_value,check_expect_flagfrom migrate_index_result where 1=1 "
                + " AND check_result_id = ? "
                );
            ps.setObject(1, migrateIndexResult.getCheckResultId());
            rs = ps.executeQuery();
            if (rs.next()) {
				result = new MigrateIndexResult();
				result.setCheckResultId(rs.getString("check_result_id"));
				result.setCheckIndexId(rs.getString("check_index_id"));
				result.setSubsCode(rs.getString("subs_code"));
				result.setCheckIndexName(rs.getString("check_index_name"));
				result.setCheckOrgCode(rs.getString("check_org_code"));
				result.setCheckOrgName(rs.getString("check_org_name"));
				result.setSourceTableId(rs.getString("source_table_id"));
				result.setSourceTableCode(rs.getString("source_table_code"));
				result.setSourceTableName(rs.getString("source_table_name"));
				result.setSourceIndexValue(rs.getBigDecimal("source_index_value"));
				result.setTargetTableId(rs.getString("target_table_id"));
				result.setTargetTableCode(rs.getString("target_table_code"));
				result.setTargetTableName(rs.getString("target_table_name"));
				result.setTargetIndexValue(rs.getBigDecimal("target_index_value"));
				result.setCheckExpectFlag(rs.getString("check_expect_flag"));
			}
        } catch (SQLException e) {
            throw new SQLException("update MigrateIndexResult is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	* 根据 migrateIndexResult 参数列表查询 不分页
	* @return int
	*/
    public List<MigrateIndexResult> queryAll(MigrateIndexResult migrateIndexResult) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<MigrateIndexResult> result = new ArrayList<>();
        try {
            String sql = "select check_result_id,check_index_id,subs_code,check_index_name,check_org_code,check_org_name,source_table_id,source_table_code,source_table_name,source_index_value,target_table_id,target_table_code,target_table_name,target_index_value,check_expect_flagfrom migrate_index_result where 1=1 ";
                if (migrateIndexResult.getCheckResultId() != null) {
                    sql = sql + " AND check_result_id =  '" + migrateIndexResult.getCheckResultId() + "'";
                }
                if (migrateIndexResult.getCheckIndexId() != null) {
                    sql = sql + " AND check_index_id =  '" + migrateIndexResult.getCheckIndexId() + "'";
                }
                if (migrateIndexResult.getSubsCode() != null) {
                    sql = sql + " AND subs_code =  '" + migrateIndexResult.getSubsCode() + "'";
                }
                if (migrateIndexResult.getCheckIndexName() != null) {
                    sql = sql + " AND check_index_name =  '" + migrateIndexResult.getCheckIndexName() + "'";
                }
                if (migrateIndexResult.getCheckOrgCode() != null) {
                    sql = sql + " AND check_org_code =  '" + migrateIndexResult.getCheckOrgCode() + "'";
                }
                if (migrateIndexResult.getCheckOrgName() != null) {
                    sql = sql + " AND check_org_name =  '" + migrateIndexResult.getCheckOrgName() + "'";
                }
                if (migrateIndexResult.getSourceTableId() != null) {
                    sql = sql + " AND source_table_id =  '" + migrateIndexResult.getSourceTableId() + "'";
                }
                if (migrateIndexResult.getSourceTableCode() != null) {
                    sql = sql + " AND source_table_code =  '" + migrateIndexResult.getSourceTableCode() + "'";
                }
                if (migrateIndexResult.getSourceTableName() != null) {
                    sql = sql + " AND source_table_name =  '" + migrateIndexResult.getSourceTableName() + "'";
                }
                if (migrateIndexResult.getSourceIndexValue() != null) {
                    sql = sql + " AND source_index_value =  '" + migrateIndexResult.getSourceIndexValue() + "'";
                }
                if (migrateIndexResult.getTargetTableId() != null) {
                    sql = sql + " AND target_table_id =  '" + migrateIndexResult.getTargetTableId() + "'";
                }
                if (migrateIndexResult.getTargetTableCode() != null) {
                    sql = sql + " AND target_table_code =  '" + migrateIndexResult.getTargetTableCode() + "'";
                }
                if (migrateIndexResult.getTargetTableName() != null) {
                    sql = sql + " AND target_table_name =  '" + migrateIndexResult.getTargetTableName() + "'";
                }
                if (migrateIndexResult.getTargetIndexValue() != null) {
                    sql = sql + " AND target_index_value =  '" + migrateIndexResult.getTargetIndexValue() + "'";
                }
                if (migrateIndexResult.getCheckExpectFlag() != null) {
                    sql = sql + " AND check_expect_flag =  '" + migrateIndexResult.getCheckExpectFlag() + "'";
                }
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
				MigrateIndexResult obj = new MigrateIndexResult();
				obj.setCheckResultId(rs.getString("check_result_id"));
				obj.setCheckIndexId(rs.getString("check_index_id"));
				obj.setSubsCode(rs.getString("subs_code"));
				obj.setCheckIndexName(rs.getString("check_index_name"));
				obj.setCheckOrgCode(rs.getString("check_org_code"));
				obj.setCheckOrgName(rs.getString("check_org_name"));
				obj.setSourceTableId(rs.getString("source_table_id"));
				obj.setSourceTableCode(rs.getString("source_table_code"));
				obj.setSourceTableName(rs.getString("source_table_name"));
				obj.setSourceIndexValue(rs.getBigDecimal("source_index_value"));
				obj.setTargetTableId(rs.getString("target_table_id"));
				obj.setTargetTableCode(rs.getString("target_table_code"));
				obj.setTargetTableName(rs.getString("target_table_name"));
				obj.setTargetIndexValue(rs.getBigDecimal("target_index_value"));
				obj.setCheckExpectFlag(rs.getString("check_expect_flag"));
                result.add(obj);
			}
        } catch (SQLException e) {
            throw new SQLException("update MigrateIndexResult is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
    protected void close(ResultSet theRs, Statement theStmt, PreparedStatement thePs) {
        try {
            if (theRs != null)
                theRs.close();
            if (theStmt != null)
                theStmt.close();
            if (thePs != null)
                thePs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
